﻿using System;
using System.Management;
using System.Collections.Generic;
using System.Text;

namespace PmfWeb.Core.Utility
{
    ///<summary>
    /// 硬盘实用类
    ///</summary>
    public static class HardWareUtils
    {
        #region 获取服务器主机名 public static string HostName()
        /// <summary>
        /// 获取主机名
        /// </summary>
        /// <returns></returns>
        public static string GetHostName()
        {
            return System.Net.Dns.GetHostName();
        }
        #endregion

        #region 获取CPU序列号 public static string CPUID()
        /// <summary>
        /// 获取CPU序列号
        /// </summary>
        /// <returns></returns>
        public static string GetCPUID()
        {
            try
            {
                ManagementClass manage = new ManagementClass("Win32_Processor");
                ManagementObjectCollection collection = manage.GetInstances();
                string cpuid = string.Empty;
                foreach (ManagementObject o in collection)
                {
                    cpuid = o.Properties["ProcessorId"].Value.ToString();
                    break;
                }
                return cpuid;
            }
            catch (Exception)
            {
                return "";
            }
        }
        #endregion

        #region 获取硬盘序列号 public static string HardDiskID()
        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetHardDiskID()
        {
            string result = "";
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                ManagementObjectCollection collection = searcher.Get();
                foreach ( ManagementObject o in collection)
                {
                    result = o["SerialNumber"].ToString().Trim();
                    break;
                }
                return result;
            }
            catch (Exception)
            {
                return result;
            }
        }
        #endregion
    }
}